package com.zhinan.controller;

import com.google.code.kaptcha.Producer;
import com.zhinan.common.R;
import com.zhinan.common.SysConstant;
import com.zhinan.utils.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @Description: TuTu_WorkManager
 * @Author: ZhiNan
 * @Date: 2023-04-21 15:03
 * @Version: 1.0
 */
@Slf4j
@Controller
@RequestMapping("/kaptcha")
@Api(tags = "验证码API", description = "获取验证码图片")
public class KaptchaController {

    @Resource(name = "captchaProducer")
    private Producer captchaProducer;

    @Resource(name = "captchaProducerMath")
    private Producer captchaProducerMath;

    @Value("${kaptchaType}")
    private String kaptchaType;


    @ApiOperation(value = "获取验证码图片")
    @GetMapping("/kaptchaImage")
    public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) {
        ServletOutputStream outputStream = null;
        try {
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
            response.setHeader("Pragma", "no-cache");
            response.setContentType("image/jpeg");
            String kaptchStr = null;
            String code = null;
            BufferedImage bufferedImage = null;
            //验证码类型为算数类型
            if ("math".equals(kaptchaType)) {
                String text = captchaProducerMath.createText();
                kaptchStr = text.substring(0, text.lastIndexOf("@"));
                code = text.substring(text.lastIndexOf("@") + 1); //拿到算数计算后的结果保存到Session中
                bufferedImage = captchaProducerMath.createImage(kaptchStr);
            } else if ("char".equals(kaptchaType)) {  //普通字符串类型
                kaptchStr = code = captchaProducer.createText();
                bufferedImage = captchaProducerMath.createImage(kaptchStr);
            }
            SessionUtil.setObjectToSession(SysConstant.KAPTCHA_SESSION_KEY, code);
            outputStream = response.getOutputStream();
            ImageIO.write(bufferedImage, "jpg", outputStream);
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;

    }
}
